package com.bh.weapp.controller.customer;

import com.bh.common.annotation.Log;
import com.bh.common.constant.Entity;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.order.domain.JcPresco;
import com.bh.order.service.IJcPrescoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 客户预录单Controller
 *
 * @author slx
 * @date 2024-03-21
 */
@RestController
@RequestMapping("/api/cust/presco")
public class JcPrescoCustApiController extends BaseController {
    @Autowired
    private IJcPrescoService jcPrescoService;

    /**
     * 查询客户预录单列表
     */
    @ApiOperation("查询客户预录单列表")
    @GetMapping("/list")
    public TableDataInfo list(JcPresco jcPresco) {
        startPage();
        List<JcPresco> list = jcPrescoService.selectJcPrescoList(jcPresco);
        return getDataTable(list);
    }

    /**
     * 获取客户预录单详细信息
     */
    @ApiOperation("获取客户预录单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcPrescoService.selectJcPrescoById(id));
    }

    /**
     * 新增客户预录单
     */
    @ApiOperation("新增客户预录单")
    @PreAuthorize("@ss.hasPermi('order:presco:add')")
    @Log(title = "客户预录单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcPresco jcPresco) {
        return toAjax(jcPrescoService.insertJcPresco(jcPresco));
    }

    /**
     * 修改客户预录单
     */
    @ApiOperation("修改客户预录单")
    @PreAuthorize("@ss.hasPermi('order:presco:edit')")
    @Log(title = "客户预录单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcPresco jcPresco) {
        // 只能修改未审核的
        if (jcPresco.getStatus() != Entity.ACTIVE) {
            return error("只能修改未审核的预录单");
        }
        return toAjax(jcPrescoService.updateJcPresco(jcPresco));
    }

    /**
     * 删除客户预录单
     */
    @ApiOperation("删除客户预录单")
    @PreAuthorize("@ss.hasPermi('order:presco:remove')")
    @Log(title = "客户预录单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        List<JcPresco> jcPrescos = jcPrescoService.listByIds(Arrays.asList(ids));
        for (JcPresco jcPresco : jcPrescos) {
            if (jcPresco.getStatus() != Entity.ACTIVE) {
                return error("只能删除未审核的预录单");
            }
        }
        return toAjax(jcPrescoService.deleteJcPrescoByIds(ids));
    }
}
